package blyth;

import java.awt.Point;
import java.util.ArrayList;

/*
 * Beliefs
 * All the variables from an agent's perceptions.
 */
public class Beliefs {

    public boolean inWater;
    public int teamTimeLeft;
    public int energy;
    public int dir;
    //public int elpasedTime;
    public int team;
    public int id;
    public Point pos;
    //public String name;
    public ArrayList<Food> foods;
    //public String[] teammates;
    public ArrayList<Agent> agents;
    //public int attackerID;

    public Beliefs(String BB) {
        this.inWater = false;
        pos = new Point();
        //name = new String();
        foods = new ArrayList<Food>();
        agents = new ArrayList<Agent>();
        //attackerID = -1;

        boolean stuffNotFound[] = new boolean[13];
        for (int i = 0; i < 13; i++) {
            stuffNotFound[i] = true;
        }

        for (int i = 0; i < BB.length(); i++) {
            String sub = BB.substring(i);

            if (stuffNotFound[0] && sub.startsWith("food(")) {
                stuffNotFound[0] = false;
                int end = FindNext(sub, ')');
                i += end - 1;
                ProcessFoods(sub.substring(5, end));
            } else if (stuffNotFound[1] && sub.startsWith("myteamtimeleft(")) {
                stuffNotFound[1] = false;
                int end = FindNext(sub, ')');
                i += end - 1;
                teamTimeLeft = Integer.parseInt(sub.substring(15, end));
            } else if (stuffNotFound[2] && sub.startsWith("inwater")) {
                stuffNotFound[2] = false;
                i += 7;
                inWater = true;
            } else if (stuffNotFound[3] && sub.startsWith("myenergy(")) {
                stuffNotFound[3] = false;
                int end = FindNext(sub, ')');
                i += end - 1;
                energy = Integer.parseInt(sub.substring(9, end));
//            } else if (stuffNotFound[4] && sub.startsWith("teammates(")) {
//                stuffNotFound[4] = false;
//                int end = FindNext(sub, ')');
//                i += end - 1;
//                teammates = sub.substring(10, end);
            } else if (stuffNotFound[5] && sub.startsWith("mydir(")) {
                stuffNotFound[5] = false;
                dir = Integer.parseInt(sub.substring(6, 7));
//            } else if (stuffNotFound[6] && sub.startsWith("time(")) {
//                stuffNotFound[6] = false;
//                int end = FindNext(sub, ')');
//                i += end - 1;
//                elpasedTime = Integer.parseInt(sub.substring(5, end));
            } else if (stuffNotFound[7] && sub.startsWith("myteam(")) {
                stuffNotFound[7] = false;
                team = Integer.parseInt(sub.substring(7, 8));
            } else if (stuffNotFound[8] && sub.startsWith("myid(")) {
                stuffNotFound[8] = false;
                int end = FindNext(sub, ')');
                i += end - 1;
                id = Integer.parseInt(sub.substring(5, end));
//            } else if (stuffNotFound[9] && sub.startsWith("myname(")) {
//                stuffNotFound[9] = false;
//                int end = FindNext(sub, ')');
//                i += end - 1;
//                name = sub.substring(7, end);
            } else if (stuffNotFound[10] && sub.startsWith("agent(")) {
                stuffNotFound[10] = false;
                int end = FindNext(sub, ')');
                i += end - 1;
                ProcessAgents(sub.substring(6, end));
//            } else if (stuffNotFound[11] && sub.startsWith("lastattacker([")) {
//                stuffNotFound[11] = false;
//                if (!sub.startsWith("lastattacker([]")) {
//                    int end = FindNext(sub, ',');
//                    i += end - 1;
//                    attackerID = Integer.parseInt(sub.substring(14, end));
//                }
            } else if (stuffNotFound[12] && sub.startsWith("mypos(")) {
                stuffNotFound[12] = false;
                int middle = FindNext(sub, ',');
                int x = Integer.parseInt(sub.substring(6, middle));
                int y = Integer.parseInt(sub.substring(middle + 1, FindNext(sub, ')')));
                pos.setLocation(x, y);
            }
        }
    }

    /*
     * FindNext
     * Returns how far away is the next specified char (end) from
     * the begining of the string.
     */
    private int FindNext(String s, char end) {
        int i;
        for (i = 0; s.charAt(i) != end; i++);
        return i;
    }

    private int FindNext(String s, char end, int begin) {
        int i;
        for (i = begin; s.charAt(i) != end; i++);
        return i;
    }

    private void ProcessFoods(String foodText) {
        if (foodText.charAt(0) == '[') {
            int i = 1;
            while (foodText.charAt(i) != ']') {
                if (foodText.charAt(i) == ',') {
                    i++;
                }
                i++;
                int distance;
                int value;
                int p_x, p_y;

                int nextComa = FindNext(foodText, ',', i);
                distance = Integer.parseInt(foodText.substring(i, nextComa));
                i = nextComa + 1;
                nextComa = FindNext(foodText, ',', i);
                value = Integer.parseInt(foodText.substring(i, nextComa));
                i = nextComa + 1;
                nextComa = FindNext(foodText, ',', i);
                p_x = Integer.parseInt(foodText.substring(i, nextComa));
                i = nextComa + 1;
                nextComa = FindNext(foodText, ']', i);
                p_y = Integer.parseInt(foodText.substring(i, nextComa));
                i = nextComa + 1;

                foods.add(new Food(value, new Point(p_x, p_y)));
            }
        }

    }

    private void ProcessAgents(String agentText) {
        if (agentText.charAt(0) == '[') {
            int i = 1;
            while (agentText.charAt(i) != ']') {
                if (agentText.charAt(i) == ',') {
                    i++;
                }
                i++;
                int distance;
                int a_id;
                int a_team;
                int a_energy;
                int p_x, p_y;
                int orientation;

                int nextComa = FindNext(agentText, ',', i);
                distance = Integer.parseInt(agentText.substring(i, nextComa));
                i = nextComa + 1;
                nextComa = FindNext(agentText, ',', i);
                a_id = Integer.parseInt(agentText.substring(i, nextComa));
                i = nextComa + 1;
                nextComa = FindNext(agentText, ',', i);
                a_team = Integer.parseInt(agentText.substring(i, nextComa));
                i = nextComa + 1;
                nextComa = FindNext(agentText, ',', i);
                a_energy = Integer.parseInt(agentText.substring(i, nextComa));
                i = nextComa + 1;
                nextComa = FindNext(agentText, ',', i);
                p_x = Integer.parseInt(agentText.substring(i, nextComa));
                i = nextComa + 1;
                nextComa = FindNext(agentText, ',', i);
                p_y = Integer.parseInt(agentText.substring(i, nextComa));
                i = nextComa + 1;
                nextComa = FindNext(agentText, ']', i);
                orientation = Integer.parseInt(agentText.substring(i, nextComa));
                i = nextComa + 1;

                agents.add(new Agent(a_id, a_team, a_energy, new Point(p_x, p_y), orientation));
            }
        }
    }
}